/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package meatslope2.gameengine.gameobjects;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11Ext;

import meatslope2.gameengine.components.Component;
import meatslope2.gameengine.engine.Engine.Time;
import meatslope2.gameengine.primatives.Vector2;

/**
 * Base class defining the core set of information necessary to render (and move
 * an object on the screen).
 * 
 */
public class GameObject 
{
    protected final Vector2 position = new Vector2();
	public Vector2 getPosition()
	{
		return this.position;
	}
	
	protected final Vector2 prevPosition = new Vector2();
	public Vector2 getPrevPosition()
	{
		return this.prevPosition;
	}
	
    protected final Vector2 velocity = new Vector2(0,0);
	public Vector2 getVelocity()
	{
		return this.velocity;
	}

    // Size
    protected final int width;
    public int getWidth()
    {
    	return this.width;
    }
    
    protected final int height;
    public int getHeight()
    {
    	return this.height;
    }
    
    private int state = GameObjectState.STATE_ALIVE;
    public int getState()
    {
    	return this.state;
    }
    
    protected final Sprite sprite;
    public Sprite getSprite()
    {
    	return this.sprite;
    }
    
    protected Component[] components = new Component[0];
    
    public GameObject(int resourceId, int width, int height)
    {
    	this.sprite = new Sprite(resourceId);
    	
    	this.width = width;
    	this.height = height;
    }
    
    private final int speed = 100;
    public void update()
    { 	
    	float deltaTime = Time.getDeltaTime();
    	position.add(velocity.x * deltaTime * speed, velocity.y * deltaTime * speed);
    	
    	for(int i = 0; i < components.length; i++)
    		components[i].update();
    	
    	this.prevPosition.set(position);
    }
    
    public void draw(GL10 pGL)
    {
    	sprite.draw(pGL);
    }
    
    public void destroy()
    {
    	this.state = GameObjectState.STATE_DEAD;
    }
    
    public boolean isDead()
    {
    	return this.state == GameObjectState.STATE_DEAD;
    }
    
    public class Sprite
    {
        // The OpenGL ES texture handle to draw.
        private int mTextureName;
        public void setTextureName(int name) 
        { 
        	this.mTextureName = name;
        }

        public int getTextureName() 
        {
            return this.mTextureName;
        }
        
        // The id of the original resource that mTextureName is based on.
        private int mResourceId;
        public void setResourceId(int id) 
        {
            this.mResourceId = id;
        }

        public int getResourceId() 
        {
            return this.mResourceId;
        }

        private Sprite(int resourceId) 
        {
            this.mResourceId = resourceId;
        }

        public void draw(GL10 gl) 
        {
            gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureName);
            
            // Draw using the DrawTexture extension.
            ((GL11Ext) gl).glDrawTexfOES(position.x, position.y, 0, width, height);
        }
    }
}